package com.yhxd.modular.reference.entity;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.yhxd.modular.system.entity.Config;
import com.yhxd.utils.RedissonUtils;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import org.hibernate.annotations.GenericGenerator;
import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.annotation.LastModifiedDate;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;

import javax.persistence.*;
import java.io.Serializable;
import java.text.DecimalFormat;
import java.util.Date;
import java.util.Map;

/**
 * 公共文件
 */
@Data
@Entity(name = "ref_pub_file")
@EntityListeners(value = AuditingEntityListener.class)
public class PublicFile implements Serializable {

    @ApiModelProperty(value = "id")
    @Id
    @GeneratedValue(generator = "idGenerator")
    @GenericGenerator(name = "idGenerator", strategy = "com.yhxd.utils.IdUtils")
    @Column(name = "id")
    private Long id;

    @ApiModelProperty(value = "目录id")
    @Column(name = "dir_id", insertable = false, updatable = false)
    private Long dirId;

    @ApiModelProperty(value = "目录id")
    @JsonIgnore
    @ManyToOne(cascade = CascadeType.REFRESH, fetch = FetchType.LAZY)
    @JoinColumn(name = "dir_id")
    @org.hibernate.annotations.ForeignKey(name = "none")
    private Directory directory;

    @ApiModelProperty(value = "文件路径")
    @Column(name = "path")
    private String path;

    @ApiModelProperty(value = "文件标题")
    @Column(name = "file_name")
    private String fileName;

    @ApiModelProperty(value = "文件重命名")
    @Column(name = "new_file_name")
    private String newFileName;

    @ApiModelProperty(value = "文件后缀")
    @Column(name = "suffix")
    private String suffix;

    @ApiModelProperty(value = "文件大小")
    @Column(name = "fileSize")
    private Long fileSize;

    @ApiModelProperty(value = "文件备注")
    @Column(name = "note")
    private String note;

    @ApiModelProperty(value = "创建人")
    @Column(name = "create_name")
    private String createName;

    @ApiModelProperty(value = "创建时间")
    @Temporal(TemporalType.TIMESTAMP)
    @CreatedDate
    @Column(name = "create_time")
    private Date createTime;

    @ApiModelProperty(value = "修改人")
    @Column(name = "update_name")
    private String updateName;

    @ApiModelProperty(value = "修改时间")
    @Temporal(TemporalType.TIMESTAMP)
    @LastModifiedDate
    @Column(name = "update_time")
    private Date updateTime;

    @ApiModelProperty(value = "数据是否可用，0不可用，1可用")
    @Column(name = "enable")
    private Integer enable = 1;

    @ApiModelProperty(value = "版本号")
    @Column(name = "version")
    private Integer version;

    @Transient
    private String dirNameStr;

    @Transient
    private String localpath;

    @Transient
    private String url;

    @Transient
    private String fileSizeStr;

    public String getDirNameStr() {
        return this.directory.getDirPath() + this.directory.getDirName();
    }

    public String getLocalpath() {
        Map<String, Config> configMap = (Map<String, Config>) RedissonUtils.getObject("config");
        String basePath = configMap.get("upload_mng").getConfValue();
        return (basePath + this.path + this.fileName + "." + this.suffix).replace("//", "/");
    }

    public String getUrl() {
        Map<String, Config> configMap = (Map<String, Config>) RedissonUtils.getObject("config");
        String basePath = configMap.get("download_mng").getConfValue();
        return basePath + this.path + this.fileName + "." + this.suffix;
    }

    public String getFileSizeStr() {
        if (null != this.fileSize) {
            DecimalFormat df = new DecimalFormat("#.00");
            String fileSizeString = "";
            if (this.fileSize < 1024) {
                fileSizeString = df.format((double) this.fileSize) + "B";
            } else if (this.fileSize < 1048576) {
                fileSizeString = df.format((double) this.fileSize / 1024) + "K";
            } else if (this.fileSize < 1073741824) {
                fileSizeString = df.format((double) this.fileSize / 1048576) + "M";
            } else {
                fileSizeString = df.format((double) this.fileSize / 1073741824) + "G";
            }
            return fileSizeString;
        } else {
            return "未知大小";
        }
    }
}
